#导入模块
import tkinter as tk
import math
import threading
import _thread
W=tk.W
from tkinter import messagebox
count=0
#str1=tk.StringVar()
#定义函数

def e(x,y): #按钮触发函数
        for b in range(int(beginvar.get()),int(endvar.get())):
           a(b)      
        lista.insert(0,"结束")
def a(n): #计算质数
    global count
    if n > 1:       
        for i in range(2,int(math.sqrt(n))+1):
            if  (n % i)== 0:
                 return          
        listb.insert(0,str(n))
        count+=1
        if count%1000==0:
            lista.insert(0,"查找"+str(n)+"个，找到"+str(count)+"个")
    else:
         return

def g():   
    Q = messagebox.askquestion(title='开始',message=f'检查一下\n开始：{beginvar.get()}\n结束：{endvar.get()}') 
    try:
        lista.insert(0,"开始")
        _thread.start_new_thread( e ,("Thread-2", 4, ) ) #另一个线程执行
        
    except:
        print ("Error: 无法启动线程")


#窗口
window = tk.Tk()
window.title("质数计算程序")
window.geometry("700x500")
window.resizable(0,0)
label=tk.Label(window,text='质数计算程序',bg="white",font=('Arial',12))
listb  =tk.Listbox(window)          
lista = tk.Listbox(window)
b=tk.Button(window,text="计算",command=g)

#放置
listb.place(x=40,y=160, anchor=W, width=120, height=200)                   
lista.place(x=240,y=160, anchor=W, width=160, height=200)
label.place(x=420,y=80, anchor=W, width=120, height=40)
b.place(x=420,y=160, anchor=W, width=120, height=40)

beginlabel=tk.Label(window,text='开始',bg="white",font=('Arial',12))
endlabel=tk.Label(window,text='结束',bg="white",font=('Arial',12))
beginlabel.place(x=90,y=300, anchor=W, width=90, height=40)
endlabel.place(x=300,y=300, anchor=W, width=100, height=40)

beginvar=tk.StringVar()
begin=tk.Entry(window,width=100,textvariable=beginvar)
begin.place(x=180,y=300, anchor=W, width=120, height=40)

endvar=tk.StringVar()
end=tk.Entry(window,width=100,textvariable=endvar)
end.place(x=400,y=300, anchor=W, width=120, height=40)



window.mainloop()
